app.controller('DemoCtrl', ['$scope', '$http', '$timeout', function ($scope, $http, $timeout) {
    $scope.rowCollectionBasic = [
        {
            firstName: 'Laurent',
            lastName: 'Renard',
            birthDate: new Date('1987-05-21'),
            balance: 102,
            email: 'whatever@gmail.com'
        },
        {
            firstName: 'Blandine',
            lastName: 'Faivre',
            birthDate: new Date('1987-04-25'),
            balance: -2323.22,
            email: 'oufblandou@gmail.com'
        },
        {
            firstName: 'Francoise',
            lastName: 'Frere',
            birthDate: new Date('1955-08-27'),
            balance: 42343,
            email: 'raymondef@gmail.com'
        }
    ];

    $scope.removeRow = function (row) {
        var index = $scope.rowCollectionBasic.indexOf(row);
        if (index !== -1) {
            $scope.rowCollectionBasic.splice(index, 1);
        }
    };

    $scope.predicates = ['firstName', 'lastName', 'birthDate', 'balance', 'email'];
    $scope.selectedPredicate = $scope.predicates[0];

    var firstnames = ['Laurent', 'Blandine', 'Olivier', 'Max'];
    var lastnames = ['Renard', 'Faivre', 'Frere', 'Eponge'];
    var dates = ['1987-05-21', '1987-04-25', '1955-08-27', '1966-06-06'];
    var id = 1;

    function generateRandomItem(id) {

        var firstname = firstnames[Math.floor(Math.random() * 3)];
        var lastname = lastnames[Math.floor(Math.random() * 3)];
        var birthdate = dates[Math.floor(Math.random() * 3)];
        var balance = Math.floor(Math.random() * 2000);

        return {
            id: id,
            firstName: firstname,
            lastName: lastname,
            birthDate: new Date(birthdate),
            balance: balance
        }
    }

    $scope.rowCollection = [];

    for (id; id < 5; id++) {
        $scope.rowCollection.push(generateRandomItem(id));
    }

    //copy the references (you could clone ie angular.copy but then have to go through a dirty checking for the matches)
    $scope.displayedCollection = [].concat($scope.rowCollection);

    //add to the real data holder
    $scope.addRandomItem = function addRandomItem() {
        $scope.rowCollection.push(generateRandomItem(id));
        id++;
    };

    //remove to the real data holder
    $scope.removeItem = function (row) {
        var index = $scope.rowCollection.indexOf(row);
        if (index !== -1) {
            $scope.rowCollection.splice(index, 1);
        }
    }

    //  pagination
    $scope.itemsByPage = 10;
    $scope.totalPages = 0;

    $scope.rowCollectionPage = [];
    for (var j = 0; j < 200; j++) {
        $scope.rowCollectionPage.push(generateRandomItem(j));
    }

    // pip
    var promise = null;
    $scope.isLoading = false;
    $scope.rowCollectionPip = [];
    $scope.getPage = function () {
        $scope.rowCollectionPip = [];
        for (var j = 0; j < 20; j++) {
            $scope.rowCollectionPip.push(generateRandomItem(j));
        }
    }

    $scope.callServer = function getData(tableState) {
        //here you could create a query string from tableState
        //fake ajax call
        $scope.isLoading = true;

        var pagination = tableState.pagination;

        var start = pagination.start || 1;     // This is NOT the page number, but the index of item in the list that you want to use to display the table.
        var number = pagination.number || 10;  // Number of entries showed per page.

        $http.post("sys/resource", {pageNum: start, pageSize: $scope.itemsByPage}).success(function (data) {
            console.log(data);
            $scope.isLoading = false;
            if (data.status == 1) {
                if (data.totalPages > 5) {
                    $scope.totalPages = 5;
                } else {
                    $scope.totalPages = data.totalPages;
                }
                var numberOfPages = Math.ceil(data.total / number);
                pagination.numberOfPages = numberOfPages;//set the number of pages so the pagination can update
                $scope.rowCollectionPip = [];
                $scope.rowCollectionPip = data.records;
            }
        });
        /*
         $timeout(function () {
         $scope.getPage();
         $scope.isLoading = false;
         }, 2000);
         */
    };

    //$scope.getPage();

}]);